<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" charset="utf-8" />
    <title>图纸管理</title>
    <link href="../../Scripts/jquery-easyui/themes/bootstrap/easyui.css" rel="stylesheet" />
    <script src="../../Scripts/jquery-easyui/jquery.min.js"></script>
    <script src="../../Scripts/jquery-easyui/jquery.easyui.min.js"></script>
    <script src="../../Scripts/jquery-easyui/locale/easyui-lang-zh_CN.js"></script>
    <link href="../../Content/themes/icon.css" rel="stylesheet" />
    <link href="../../Content/themes/table.css" rel="stylesheet" />
    <link href="../../Content/formstyles.css" rel="stylesheet" />
    <script src="../../Content/js/common.js"></script>  
    <script src="../../Content/js/commonBase.js"></script>  
    <script src="../../Content/js/appendhandle.js"></script>
</head>
<body>
    <div id="ui_pm_layout" class="easyui-layout" data-options="fit:true,border:false">   
        <div data-options="region:'center',border:false">			
            <div class="easyui-panel" title="基本信息" style="width:100%;height:auto;padding:10px;">

              <div style="margin-bottom: 2px;"> 
                    <input type="text" class="easyui-textbox" id="ProjectNo" data-options="width:200, label:'项目编码',labelPosition:'before',labelWidth:80,labelAlign:'right',readonly:true" />
                    <input type="text" class="easyui-textbox" id="ProjectName" data-options="width:300,label:'项目名称',labelPosition:'before',labelWidth:80,labelAlign:'right',readonly:true" />
            </div> 
                <div >
                    <input type="text" class="easyui-textbox" id="ProjectManager" data-options="width:200,label:'项目管理员',labelPosition:'before',labelWidth:80,labelAlign:'right',readonly:true" />
                    <input type="text" class="easyui-textbox" id="ProjectClerk" data-options="width:200,label:'业务员',labelPosition:'before',labelWidth:80,labelAlign:'right',readonly:true" />
                    <input class="easyui-combobox" id="comDepartmentList" name="comDepartmentList" data-options="width:200,label:'部门',labelPosition:'before',labelWidth:80,labelAlign:'right', required:true, valueField: 'DepartmentName', textField: 'DepartmentName',url: '/Project/SelectDepartment',readonly:true ">
  </div>  </div>

            <div class="easyui-panel" title="项目设计" style="width:100%;height:auto;padding:5px;"> 
                               
              <div style="margin-bottom: 2px;">
             <input type="text" class="easyui-textbox" id="bsExecutor" data-options="width:200,label:'执行人',labelPosition:'before',labelWidth:80,labelAlign:'right',disabled:false" />
                <input type="text" id="bsPSTime" class="easyui-datebox" data-options="width:200,label:'计划工期',labelPosition:'before',labelAlign:'right'" /> <input type="text" id="bsPETime" class="easyui-datebox" data-options="width:150,label:'至 ',labelWidth:20,labelPosition:'before',labelAlign:'right'" />
              <input type="text" id="bsRSTime" class="easyui-datebox" data-options="width:200,label:'实际工期',labelPosition:'before',labelAlign:'right'" /> <input type="text" id="bsRETime" class="easyui-datebox" data-options="width:150,label:'至 ',labelWidth:20,labelPosition:'before',labelAlign:'right'" />
                 
                      <a id="btn-save" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">保存</a> 
              
               </div>
                     
     <div>  
                         <div style="float:left">
                             <table id="dg_draw" class="easyui-datagrid" style="width:600px;height:250px"
                                    data-options="title:'图纸', singleSelect:true,collapsible:true,">
                                 <thead>
                                     <tr>
                                         <th data-options="field:'AppendID',hidden: true">ID</th>
                                         <th data-options="field:'AppendPath',hidden: true">path</th>
                                         <th data-options="field:'AppendName',width:220">文件名称</th>
                                         <th data-options="field:'UpdateTime',width:140,align:'right'">上传时间</th>
                                         <th data-options="field:'UpdateBy',width:80,align:'right'">上传人</th>
                                     </tr>
                                 </thead>
                             </table>        
                            
    </div> 
	 <div style="float:left;margin-left: 5px;">
	                      
        <table id="bsdatagrid" class="easyui-datagrid" style=" width:400px;height:250px"
               data-options="title:'附件', singleSelect:true,collapsible:true,toolbar: '#tb'">
            <thead>
                <tr>
                    <th data-options="field:'AppendID',hidden: true">ID</th>
                    <th data-options="field:'AppendPath',hidden: true">path</th>
                    <th data-options="field:'AppendName',width:220">文件名称</th>
                    <th data-options="field:'UpdateTime',width:140,align:'right'">上传时间</th>
                    <th data-options="field:'UpdateBy',width:80,align:'right'">上传人</th>
                </tr>
            </thead>
        </table>
	</div>   
	</div>
	<div class="clear"></div>
	  <div  style="margin-top: 2px;" >
       <input id="bsRemark" class="easyui-textbox" data-options="width:500,height:30,label:'备注',labelWidth:40,labelPosition:'before',labelAlign:'right',multiline:true">
     </div>           
          
            </div>
        </div>
    </div>
	
	<div id="dlg" class="easyui-dialog" style="width:400px" data-options="closed:true,modal:true,border:'thin',buttons:[{
			text:'下发图纸',
			iconCls:'icon-ok',
			handler:function(){saveEntry()}
		},{
			text:'取消',
			iconCls:'icon-cancel',
			handler:function(){$('#dlg').dialog('close')}
		}]">
	    <form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
	         <div style="margin-bottom:2px">
	            <input class="easyui-textbox" name="subItemCode" id="subItemCode" data-options="label:'子物料代码:', required:true," style=" width:100%">
	        </div>
	        <div style="margin-bottom:2px">
	            <input name="subItemName" id="subItemName" class="easyui-textbox" label="子物料名称:" style="width:100%" data-options="editable:false,">
	        </div> 
			<div style="margin-bottom:10px">
			    <input name="drawCount" id="drawCount" class="easyui-textbox" label="图纸数量:" style="width:100%" data-options="required:true,">
			</div>  
		 <div style="margin-bottom:10px">
	            <input name="d_remark" id="d_remark" class="easyui-textbox" label="备注:" style="width:100%">
	        </div>
	    </form>
	</div>
	
	<script type="text/javascript">
        var ProjectID = getQueryVariable("ProjectID") || "";//获取项目id
        var project;
        var lock = false;
	$(function(){
		//查询并获取项目基础数据
	    $.ajax({
	        url: "/Project/GetPMList?&r=" + Math.random(),
			data:{
				"ProjectID":ProjectID,
				
			},
			dataType:'json',//服务器返回json格式数据 
			timeout:5000,//超时时间设置为10秒；
			success:function(data){
				//ProjectID,ProjectNo,ProjectName,ProjectManager,ProjectClerk,CreateBy,CreateTime,UpdateTime,UpdateBy,Remark,status,Department,IcmoNo,FitemNo,FName,FModel
			    project = data.rows[0];
			    init(project);//初始化视图
			},
			error:function(xhr,type,errorThrown){
				$.show_alert("获取项目信息失败:"+ProjectID);
			}
		});
	});
	
  //页面主元素赋值与初始化
	function init(row) { 
	    //$("#ProjectID").textbox('setValue', row.ProjectID);		
	    $("#ProjectNo").textbox('setValue', row.ProjectNo);
	    $("#ProjectName").textbox('setValue', row.ProjectName);
	    $("#ProjectManager").textbox('setValue', row.ProjectManager);
	    $("#ProjectClerk").textbox('setValue', row.ProjectClerk);
	    $("#comDepartmentList").combobox('setValue', row.Department);
	 //获取并绑定已有的设计节点信息与附件信息
	 $.getJSON("/Project/GetPMNodeInfo", { ProjectID: row.ProjectID, NodeID: 4 }, function (data) {
	        if (data.success) {
	            nodedata = data.rows[0];
	            $("#bsExecutor").textbox('setValue', nodedata.executor || "");
	            $("#bsPSTime").datebox('setValue', nodedata.PSTime.replace(/\//g, '-'));//将全局的\号替换为-，使得js能识别为日期格式
	            $("#bsPETime").datebox('setValue', nodedata.PETime.replace(/\//g, '-'));
	            $("#bsRSTime").datebox('setValue', nodedata.RSTime.replace(/\//g, '-'));
	            $("#bsRETime").datebox('setValue', nodedata.RETime.replace(/\//g, '-'));
	            $("#bsRemark").textbox('setValue', nodedata.Remark);
	            //加载附件,nodedata.AppendListID
	            var aid = nodedata.AppendListID;
	            $('#bsdatagrid').datagrid({
	                url: "/Project/GetAppendList?AppendListID=" + aid + "&r=" + Math.random(),
	                toolbar: [{
	                    text: '添加',
	                    id: 'append_add_bt',
	                    iconCls: 'icon-add',
	                    handler: function () {
	                        uploadfile(aid, $("#bsdatagrid"));
	                    }
	                }, {
	                    text: '删除',
	                    id: 'append_del_bt',
	                    iconCls: 'icon-delete',
	                    handler: function () {
	                        Delfile($("#bsdatagrid"));
	                    }
	                }, {
	                    text: '下载附件',
	                    iconCls: 'icon-save',
	                    handler: function () {
	                        downloadfile($("#bsdatagrid"));
	                    }
	                }]
	            });
	
	        } else {
	            $.show_alert("提示", data.msg);
	        }
	    });
	 //获取图纸下发信息
	 $("#dg_draw").datagrid(
         {     
	            url: "/Draw/getDrawList?&r=" + Math.random(),
	            striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
	            queryParams: {
	                "projectID": ProjectID,
	            },
	            idField: 'drawId',
	            sort: 'issueTime',
	            order: 'desc',
	            pageList: [20, 40, 60, 80, 100], 
	            columns: [[
	                     { field: "drawId", width: "120", title: "下图编码", align: 'center', hidden: true },
	                     { field: "subItemCode", width: "80", title: "物料代码", align: 'center' },
	                     { field: "subItemName", width: "80", title: "物料名称", align: 'center', formatter: showLongString, },
	                     { field: "drawCount", width: "50", title: "数量", align: 'center' },
						 { field: "adjCount", width: "50", title: "增补", align: 'center' },
						 { field: "issueBy", width: "120", title: "图纸下发", align: 'center', hidden: true },
	                     { field: "issueTime", width: "100", title: "图纸下发时间", align: 'center', formatter: getDate },
	                     { field: "confirmBy", width: "70", title: "图纸确认", align: 'center' },
	                     { field: "confirmTime", width: "100", title: "图纸确认时间", align: 'center', formatter: getDate },
	                     { field: "remark", width: "200", title: "备注", align: 'center', hidden: true },
	            ]],
	            toolbar: [{
	                text: '下达图纸',
	                id: 'append_add_bt',
	                iconCls: 'icon-add',
	                handler: function () {
	                    assigndrawMgr();
	                }
	            }, {
	                text: '删除下达',
	                id: 'append_del_bt',
	                iconCls: 'icon-delete',
	                handler: function () {
	                     DeldrawMgr(); 
	                }
	            }, {
	                text: '确认图纸',
	                iconCls: 'icon-check',
	                handler: function () {
	                     confirmdrawMgr();
	                }
	            }, {
	                text: '取消确认',
	                iconCls: 'icon-uncheck',
	                handler: function () {
	                   unconfirmdrawMgr();
	                }
	            },{
                       text: '调整数量',
                     id: 'append_add_bt',
                     iconCls: 'icon-add',
                     handler: function () {
                         adjDrawCount();
	                 }
                 },],
	            onSelect: function (rowIndex, rowData) {
	              //  menuControl();
	            },
	            onLoadSuccess: function (data) {  },
	        }); 
	 $('#btn-save').bind('click', function(){
	       $.ajax({     //提交数据保存
	               url: "/Project/EditPMNode?r=" + Math.random(),
	               type: "post",
	               data: {
	                   "ProjectID": row.ProjectID,
	                   "NodeID": 4,
	                   "PSTime": $("#bsPSTime").datebox('getValue'),
	                   "PETime": $("#bsPETime").datebox('getValue'),
	                   "RSTime": $("#bsRSTime").datebox('getValue'),
	                   "RETime": $("#bsRETime").datebox('getValue'),
	                   "Remark": $("#bsRemark").val(),
	                   "Executor": $("#bsExecutor").val(),
	               },
	               dataType: "json",
	               timeout: 5000,
	               success: function (data) {
	                   lock = false;
	                   if (data.success) {
	                       $.show_alert("成功", "保存成功！");
	                       //$("#ui_pm_bs_dialog").dialog('destroy');
	                       //$("#ui_pm_dg").datagrid("reload");//.datagrid('clearSelections').datagrid('clearChecked');
	                   } else {
	                       $.show_alert("提示", data.msg);
	                   }
	               },
	               error: function (XMLHttpRequest, textStatus, errorThrown) {
	                   if (textStatus == "timeout") {
	                       $.show_alert("提示", "请求超时，请刷新当前页重试！");
	                   }
	                   else {
	                       $.show_alert("错误", textStatus + "：" + errorThrown);
	                   }
	               }
	           })
	       }) 
	}
        //下发图纸

     function saveEntry() {        
         if (!$("#fm").form('validate')) {//表单校验
             $.show_alert("错误", "表单校验未通过！请注意录入数据格式");
             return;
         } else {
             if (lock) { return; } else { lock = true;  }
         }
         $.ajax({
             url: "/Draw/issueDraw",
             type: "post",
             data: {
                 "mainItemCode":project.FNumber,
                 //"mainItemName": $('#mainItemCode').combobox("getValue"),
                 "subItemCode": $('#subItemCode').combobox("getText"),
                 "subItemName": $('#subItemCode').combobox("getValue"),
                 "drawId": uuid(20, 10),
                 "drawCount": $('#drawCount').textbox('getValue'),
                 "remark": $('#d_remark').textbox('getValue'),
                 "projectID": ProjectID,
             },
             dataType: 'json',//html\script\text\xml\json
             beforeSend: function () { },//
             complete: function () { lock = false; },
             timeout: 1000,
             success: function (data) {
                 if (data.success) {
                     $.show_alert("成功", "下发成功！");
                     $('#fm').form('clear');
                     $('#dlg').dialog('close');
                     $("#dg_draw").datagrid("reload"); 
                 } else {
                     $.show_alert("提示", data.msg);
                 }
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {
                 if (textStatus == "timeout") {
                     $.show_alert("提示", "请求超时，请刷新当前页重试！");
                 }
                 else {
                     $.show_alert("错误", textStatus + "：" + errorThrown);
                 }
             },
            
         });
     }
	 
	 //删除，取消下发图纸
	 function DeldrawMgr() {
	     var row = $("#dg_draw").datagrid("getSelected");
	     if (row == null || row.length < 1) {
	         $.show_alert("提示", "请先勾选要删除的图纸");
	         return;
	     }
	     if (row.drawId == null || row.drawId == '') {
	         $("#dg_draw").datagrid('clearSelections').datagrid('clearChecked');
	         return;
	     }
	     if (row.confirmBy.length > 2) {
	         $.show_alert("提示", "该图纸已确认，无法删除");
	         return;
	     }
	     $.ajax({
	         url: "/Draw/delDraw",
	         type: "post",
	         data: {
	             "drawId": row.drawId,
	         },
	         dataType: 'json',//html\script\text\xml\json
	         // beforeSend: function () { },//
	         // complete: function () { lock = false; },
	         timeout: 1000,
	         success: function (data) {
	             if (data.success) {
	                 $.show_alert("成功", "删除成功！");
	                 $("#dg_draw").datagrid("reload");//.datagrid('clearSelections').datagrid('clearChecked');
	             } else {
	                 $.show_alert("提示", data.msg);
	             }
	         },
	         error: function (XMLHttpRequest, textStatus, errorThrown) {
	             if (textStatus == "timeout") {
	                 $.show_alert("提示", "请求超时，请刷新当前页重试！");
	             }
	             else {
	                 $.show_alert("错误", textStatus + "：" + errorThrown);
	             }
	         },
	     });
	 
	 
	 }
	     //确认图纸
	 function confirmdrawMgr() {
	     var row = $("#dg_draw").datagrid("getSelected");
	     if (row == null || row.length < 1) {
	         $.show_alert("提示", "请先勾选要确认的图纸");
	         return;
	     }
	     if (row.drawId == null || row.drawId == '') {
	         $("#dg_draw").datagrid('clearSelections').datagrid('clearChecked');
	         return;
	     }
	     if (row.confirmBy.length>2) {
	         $.show_alert("提示", "该图纸已确认");
	         return;
	     }
	     $.ajax({
	         url: "/Draw/confirmDraw",
	         type: "post",
	         data: {                    
	             "drawId": row.drawId,
	         },
	         dataType: 'json',//html\script\text\xml\json
	        // beforeSend: function () { },//
	        // complete: function () { lock = false; },
	         timeout: 1000,
	         success: function (data) {
	             if (data.success) {
	                 $.show_alert("成功", "确认成功！");
	                 //$('#fm').form('clear');
	                 //$('#dlg').dialog('close');
	                 $("#dg_draw").datagrid("reload");//.datagrid('clearSelections').datagrid('clearChecked');
	             } else {
	                 $.show_alert("提示", data.msg);
	             }
	         },
	         error: function (XMLHttpRequest, textStatus, errorThrown) {
	             if (textStatus == "timeout") {
	                 $.show_alert("提示", "请求超时，请刷新当前页重试！");
	             }
	             else {
	                 $.show_alert("错误", textStatus + "：" + errorThrown);
	             }
	         },
	     });
	 }
	 
	 //取消确认图纸
	 function unconfirmdrawMgr() {
	     var row = $("#dg_draw").datagrid("getSelected");
	     if (row == null || row.length < 1) {
	         $.show_alert("提示", "请先勾选要取消确认的图纸");
	         return;
	     }
	     if (row.drawId == null || row.drawId == '') {
	         $("#dg_draw").datagrid('clearSelections').datagrid('clearChecked');
	         return;
	     }
	     if (row.confirmBy==null||row.confirmBy=="") {
	         $.show_alert("提示", "该图纸未确认");
	         return;
	     }
	     $.ajax({
	         url: "/Draw/confirmDraw",
	         type: "post",
	         data: {
	             "drawId": row.drawId,
	             "type":'unConfirm'
	         },
	         dataType: 'json',//html\script\text\xml\json
	         // beforeSend: function () { },//
	         // complete: function () { lock = false; },
	         timeout: 1000,
	         success: function (data) {
	             if (data.success) {
	                 $.show_alert("成功", "取消确认成功！");
	                 $("#dg_draw").datagrid("reload");//.datagrid('clearSelections').datagrid('clearChecked');
	             } else {
	                 $.show_alert("提示", data.msg);
	             }
	         },
	         error: function (XMLHttpRequest, textStatus, errorThrown) {
	             if (textStatus == "timeout") {
	                 $.show_alert("提示", "请求超时，请刷新当前页重试！");
	             }
	             else {
	                 $.show_alert("错误", textStatus + "：" + errorThrown);
	             }
	         },
	 
	     });
	 
	 }

        //调整数量
	 function adjDrawCount() {

	     var row = $("#dg_draw").datagrid("getSelected");
	     if (row == null || row.length < 1) {
	         $.show_alert("提示", "请先选择要调整数量的物料");
	         return;
	     }
	     if (row.drawId == null || row.drawId == '') {
	         $("#dg_draw").datagrid('clearSelections').datagrid('clearChecked');
	         return;
	     }
	     if (row.confirmBy == null || row.confirmBy == "") {
	         $.show_alert("提示", "该图纸未确认");
	         return;
	     }
	     var adjCount = prompt("请输入增补的图纸数量：");

	     if(!/^\d+$/.test(adjCount)){
	         alert("输入不是正整数");
             return;
            } 
	     $.ajax({
	         url: "/Draw/adjDrawCount",
	         type: "post",
	         data: {
	             "drawId": row.drawId,
	             "adjCount": adjCount,
	         },
	         dataType: 'json',//html\script\text\xml\json 
	         timeout: 1000,
	         success: function (data) {
	             if (data.success) {
	                 $.show_alert("成功", "调整成功！");
	                 $("#dg_draw").datagrid("reload");//.datagrid('clearSelections').datagrid('clearChecked');
	             } else {
	                 $.show_alert("提示", data.msg);
	             }
	         },
	         error: function (XMLHttpRequest, textStatus, errorThrown) {
	             if (textStatus == "timeout") {
	                 $.show_alert("提示", "请求超时，请刷新当前页重试！");
	             }
	             else {
	                 $.show_alert("错误", textStatus + "：" + errorThrown);
	             }
	         },

	     });

	 }
        //下发图纸
	 function assigndrawMgr() {
	   $('#dlg').dialog('open').dialog('center').dialog('setTitle', '下发图纸'); 
	   $('#subItemCode').combobox({
	       url: '/Bom/getBomChildItem?q='+project.FNumber,
	       valueField: 'FName',
	       textField: 'FNumber',
	       formatter: function (row) {
	           var opts = $(this).combobox('options');
	           return row[opts.textField] + " " + row[opts.valueField];
	       },
	       onChange: function (newValue, oldValue) {
	           $("#subItemName").textbox('setValue', newValue);
	       }
	   });  
	     $('#fm').form('clear');
	 }
	 
	$.ajax({     //请求当前用户可以操作的按钮
	    url: "/Button/GetUserAuthorizeButton?r=" + Math.random(),
	    type: "post",
	    data: { "KeyCode": "drawMgr", "KeyName": "drawMgr" },
	    dataType: "json",
	    timeout: 5000,
	    success: function (data) {
	        if (data.success) {
	            //前端添加按钮
	            //data.toolbar.push({ "text": "查看", "id": "lookbill", "iconCls": "icon-more", "handler": "lookbill();" })
	            var toolbar = getToolBar(data);      //common.js
	            if (data.search) {     //判断是否有浏览权限 
	                $("#dg_draw").datagrid({       //初始化datagrid
	                    url: "/Draw/getDrawList?&r=" + Math.random(),
	                    striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
	                    queryParams: {
	                        "projectID": ProjectID,
	                    },
	                    idField: 'drawId',
	                    sort: 'issueTime',
	                    order: 'desc',
	                    pageList: [20, 40, 60, 80, 100],
	                    frozenColumns: [[]],
	                    columns: [[
	                     { field: "drawId", width: "120", title: "下图编码", align: 'center',hidden:true },
	                     { field: "subItemCode", width: "80", title: "物料代码", align: 'center' },
	                     { field: "subItemName", width: "80", title: "物料名称", align: 'center', formatter: showLongString, },
	                     { field: "drawCount", width: "50", title: "数量", align: 'center' },
						 { field: "adjCount", width: "50", title: "增补", align: 'center' },
						 { field: "issueBy", width: "120", title: "图纸下发", align: 'center', hidden: true },
	                     { field: "issueTime", width: "100", title: "图纸下发时间", align: 'center', formatter: getDate },
	                     { field: "confirmBy", width: "70", title: "图纸确认", align: 'center' },
	                     { field: "confirmTime", width: "100", title: "图纸确认时间", align: 'center',formatter: getDate },                          
	                     { field: "remark", width: "200", title: "备注", align: 'center', hidden: true },
	        ]],
	                    toolbar: toolbar.length == 0 ? null : toolbar,
	                    onSelect: function (rowIndex, rowData) {
	                      //  menuControl();
	                    },
	                    onLoadSuccess: function (data) {  }
	                });
	            }
	            else {
	                $.show_alert("提示", "无权限，请联系管理员！");
	            }
	        } else {
	            $.show_alert("错误", data.result);
	        }
	    },
	    error: function (XMLHttpRequest, textStatus, errorThrown) {
	        if (textStatus == "timeout") {
	            $.show_alert("提示", "请求超时，请刷新当前页重试！");
	        }
	        else {
	            $.show_alert("错误", textStatus + "：" + errorThrown);
	        }
	    }
	})
	     
	</script>
</body>
</html>


